const wxParse = require("../../../wxParse/wxParse.js");
const App = getApp();
import util from '../../../utils/util.js';
var template = require('../../template/template.js');
Page({ 

	/**
	 * 页面的初始数据
	 */
	data: {
		tabs: [{
				name: '积分兑换',
				url: "pages/jifen/exchange/index"
			},
			{
				name: '积分换购',
				url: "pages/jifen/changeBuy/index"
			},
			{
				name: '积分订单',
				url: "pages/jifen/order/index"
			},
		],
		list: [],
		searchVal: '',
		page: 1,
		lists: [],
		isSearch: false,
		intel: 0, //积分
		isRule: false,
		cont: '暂无规则'
	},
	onShow(){
		template.tabbar("tabBar", 2, this)//0表示第一个tabbar
		this.getData()
		this.openRule()
		//缓存获取积分
		this.setData({
			isLogin: wx.getStorageSync('token')?true:false
		})
	},
	navcato(e){
		wx.redirectTo({
			url: '/'+ e.currentTarget.dataset.urls
		})
	},
	// 去登陆
	goLogin(){
		wx.navigateTo({
			url: '/pages/login/login',
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
	},
	catchtouchmove() {
		return
	},
	openRule() {
		var that = this
		App._post_form('goods/integralInfo', {}, function(res) {
			console.log(res)

			if (res.code == 1) {
				// 富文本转码
				if (res.data.length > 0) {
					wxParse.wxParse('content', 'html', res.data, that, 0);
				}
				that.setData({
					cont: res.data,
					intel:  res.data.user_points
				})
			}
		})
	},
	openRules() {
		this.setData({
		  isRule:true
		})
	},
	closeRule() {
		this.setData({
			isRule: false
		})
	},
	/**
	 * 获取数据
	 */
	getData() {
		var that = this
		App._post_form('integral/index', {
			exchange: '',
			num: 7
		}, function(res) {
			console.log(res)
			let {
				changeArray
			} = util
			let list = changeArray(res.data)
			console.log(list)
			parseFloat
			that.setData({
				list
			})
		})
	},
	bindblur(e) {
		this.setData({
			searchVal: e.detail.value
		})
	},
	// 搜索
	bindconfirm(e) {
		this.setData({
			searchVal: e.detail.value,
			page: 1,
			list: [],
			lists: []
		})
		this.searchGetData()
	},
	//搜索获取数据
	searchGetData() {
		var that = this
		var page = this.data.page
		if (page == -1) {
			wx.showToast({
				title: '暂无更多数据',
				icon: 'none'
			})
			return
		}
		App._post_form('integral/search', {
			page: page,
			search: that.data.searchVal,
			category_id: ''
		}, function(res) {
			console.log(res)
			let {
				last_page,
				data
			} = res.data.list
			if (page < last_page) {
				page++
			} else {
				page = -1
			}
			let {
				changeArray
			} = util
			let lists = that.data.lists.concat(data)
			let list = changeArray(lists)
			console.log(list)
			// parseFloat
			that.setData({
				isSearch: true,
				page,
				list,
				lists
			})
		})
	},
	/**
	 * 菜单列表导航跳转
	 */
	onTargetTab(e) {
		wx.navigateTo({
			url: '/' + e.currentTarget.dataset.url
		})
	},
	/**
	 * 跳转立即兑换详情
	 */
	onTargetDetails(e) {
		wx.navigateTo({
			url: '/pages/jifen/exchangeDetails/index?j_id=' + e.currentTarget.dataset.id
		})
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (this.data.searchVal) {
			this.searchGetData()
		}


	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
